
using System.Web.UI.HtmlControls;
using System;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System.Collections;
using System.Web.UI;
using System.Web;


using com.ddsc.common;
//using com.ddsc.database;
using DDSC.OracleADO;
using com.ddsc.utility;
using log4net;
using log4net.Config;
using System.Data.OracleClient;


namespace smasb2b
{
	public class DOWN_PURCHASE_DOCUMENT : BasePage
	{
		
#region  Web Form 設計工具產生的程式碼
		
		//此為 Web Form 設計工具所需的呼叫。
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		protected System.Web.UI.HtmlControls.HtmlInputHidden hidDOCUMENT;
		
		//注意: 下列預留位置宣告是 Web Form 設計工具需要的項目。
		//請勿刪除或移動它。
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: 此為 Web Form 設計工具所需的方法呼叫
			//請勿使用程式碼編輯器進行修改。
			InitializeComponent();
		}
		
#endregion
		
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			
			try
			{
				
				
				string PURCHASE_NO = Page.Request["purchase_no"];
				string version = Page.Request["version"];
				
				//dbConnection dba = new dbConnection();
				//OracleConnection myConnection = dba.getOracleClientConnection();
				//string strSQL = string.Format("select PURCHASE_NO,DOCUMENT_TYPE,DOCUMENT from B2B_PURCHASE_NOTICE where PURCHASE_NO=\'{0}\' and version=\'{1}\' and  status=9  ", PURCHASE_NO, version);
				//OracleCommand myCommand = new OracleCommand(strSQL, myConnection);
				//OracleDataAdapter MyDA = new OracleDataAdapter(myCommand);
				//DataSet ds = new DataSet();
				//MyDA.Fill(ds);
				//myConnection.Close();
				//myConnection.Dispose();

                //Tony:參考VNDW0032將上段改寫成下段
                dbAccess dba = new dbAccess();
                ArrayList m_arrParam = new ArrayList();
                string strSQL = "select PURCHASE_NO,DOCUMENT_TYPE,DOCUMENT from B2B_PURCHASE_NOTICE where PURCHASE_NO=:PURCHASE_NO and version=:version and  status=9  ";
                m_arrParam.Add(dba.GetOracleParam(":PURCHASE_NO", PURCHASE_NO));
                m_arrParam.Add(dba.GetOracleParam(":version", version));
                DataSet ds = dba.runExecuteQuery("DATA", strSQL, m_arrParam);

				System.DBNull DBNull;
				byte[] byteBLOBData = null;
                if (!(ds.Tables["DATA"].Rows[0]["DOCUMENT"].GetType().ToString() == "System.DBNull"))
				{
					hidDOCUMENT.Value = "Y";
					//-=============================-
                    byteBLOBData = (byte[])ds.Tables["DATA"].Rows[0]["DOCUMENT"];
					// Me.Image1.ImageUrl = byteBLOBData
					string attFile = System.Convert.ToString(ds.Tables["DATA"].Rows[0]["DOCUMENT_TYPE"]);
					switch (attFile.ToUpper())
					{
						case "DOC":
							Response.ContentType = "application/msword";
							break;
						case "PDF":
							Response.ContentType = "application/pdf";
							break;
						case "XLS":
							Response.ContentType = "image/html";
							break;
						default:
							Response.ContentType = "text/html";
							break;
					}
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    String str = (dynamic)String.Format("attachment;filename={0}.{1}", PURCHASE_NO, attFile);
					Response.AddHeader("content-disposition", str);
					Response.Clear();
					Response.BinaryWrite(byteBLOBData);
				}
				else
				{
					hidDOCUMENT.Value = "N";
				}
				this.RegisterStartupScript("errMessage", "<script language=\"javascript\"> window.opener.WClose();</script>");
			}
			catch (Exception ex)
			{
				Response.Write(ex.Message);
			}
			
		}
		
	}
	
}
